package com.dzbh.cdsdriver.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.util.Log;
import com.baidu.android.pushservice.PushManager;
import com.dzbh.cdsdriver.R;
import com.dzbh.cdsdriver.location.LocationService;
import com.dzbh.cdsdriver.location.listener.MyLocationListener;

import java.util.ArrayList;
import java.util.List;


public class MyLocationService extends Service {
    private LocationService locationService;
    private MyLocationListener myListener;
    private Context mContext;
    private PowerManager.WakeLock wl=null;
    private AlarmManager am;
    private PendingIntent pi;
    private Mreceiver mreceiver;
    List<String> tags;
    LocationReceiver locationReceiver;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        tags = new ArrayList<>();
        tags.add("运输中");
        myListener = new MyLocationListener();
        myListener.setmContext(mContext);
        locationService = new LocationService(getApplicationContext());
        locationService.registerListener(myListener);

        PowerManager pm=(PowerManager) this.getSystemService(Context.POWER_SERVICE);
        wl=pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"mywakrlock");
        wl.setReferenceCounted(false);
        //注册锁屏广播，主要是解决高德在锁屏黑屏情况下定位不更新的问题
        IntentFilter intentfilter=new IntentFilter();
        intentfilter.addAction(Intent.ACTION_SCREEN_ON);
        intentfilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentfilter.addAction(Intent.ACTION_USER_PRESENT);
        mreceiver=new Mreceiver();
        registerReceiver(mreceiver,intentfilter);

        //注册设置定时唤醒定位
        IntentFilter intentFile = new IntentFilter();
        intentFile.addAction("repeating");
        locationReceiver = new LocationReceiver();
        registerReceiver(locationReceiver, intentFile);

        //写一个定时的Pendingintent
        Intent intent = new Intent();
        intent.setAction("repeating");
        pi = PendingIntent.getBroadcast(this, 0, intent, 0);
        am = (AlarmManager)getSystemService(ALARM_SERVICE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locationService.unregisterListener(myListener);
        PushManager.delTags(mContext, tags);
        locationService.stop();
        stopForeground(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        locationService.start();
        PushManager.setTags(mContext, tags);
        Notification notification = new Notification.Builder(this.getApplicationContext())
        .setContentText("这是一个前台服务")
         .setContentTitle("司机客户端启用")
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_icon))
        .setWhen(System.currentTimeMillis())
        .build();
        startForeground(110, notification);
        Log.d("dong", "MyLS onStartCommand");
        return START_STICKY;
    }

    public class Mreceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action =intent.getAction();
            //开屏
            if(Intent.ACTION_SCREEN_ON.equals(action)){
                Log.d("dong","开屏");
            }//锁屏
            else if(intent.ACTION_SCREEN_OFF.equals(action)){
                Log.d("dong","锁屏");
                //如果锁屏关闭当前常规定位方法，调用alarm,每2秒发动一次单次定位
                //locationClient.stopLocation();
                wake();


            }//解锁
            else if(intent.ACTION_USER_PRESENT.equals(action)){
                Log.d("dong","解锁");
                am.cancel(pi);
                wl.release();
            }
        }

        private void wake() {
            // TODO Auto-generated method stub
            //每2秒激活广播，发起一次定位
            // startLocation(true);
            wl.acquire();
            am.setRepeating(0, System.currentTimeMillis(),2000, pi);
        }
    }

    class LocationReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("dong","定位重新获取");
            Intent intent1 = new Intent(mContext, MyLocationService.class);
            startService(intent1);
        }
    }
}
